package com.slf.engine.threads;

import org.apache.log4j.Logger;
import org.apache.log4j.Priority;

import com.slf.engine.bo.Order;
import com.slf.engine.common.SysContext;
import com.slf.engine.services.IBusService;
import com.slf.engine.services.impl.BusServiceImpl;

/**
 * 
 * @author ZhangDong
 * @version 2.0
 * @category 业务完成以后，进行通知反馈
 *
 */
public class NoticeThread extends Thread {

	private static final Logger log = Logger.getLogger(NoticeThread.class);
	
	public NoticeThread()
	{
		log.log(Priority.INFO, "NOTICE_THREAD RUN SUCCESS");
	}
	
	public void run()
	{
		IBusService busService = new BusServiceImpl();
		while(true)
		{
			try{
				Order order = null;
				synchronized (SysContext.NOTICE_LIST) {
					if(SysContext.NOTICE_LIST.size() > 0)
					{
						order = SysContext.NOTICE_LIST.get(0);
						if(System.currentTimeMillis() >= order.getNexttzsj())
						{
							SysContext.NOTICE_LIST.remove(order);
						}
					}
				}
				if(order != null)
				{
					busService.doNoticeResult(order);
				}else{
					log.log(Priority.INFO, "当前没有要通知的对象，等待3秒钟后重试");
					try {
						Thread.sleep(3000);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
					}
				}
			}catch (Exception e) {
				// TODO: handle exception
				log.log(Priority.ERROR, "通知异常：" + e.getMessage());
			}
		}
	}
}
